<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="600"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{'top':'93px','left':'154px'}"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
      style="top:10%;height: 80%;">
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel">
          关闭
        </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="contractModal">

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同编号">
              <a-input placeholder="输入合同编号" v-decorator.trim="[ 'sn', validatorRules.sn ]" />
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同名称">
              <a-input placeholder="输入合同名称" v-decorator.trim="[ 'name', validatorRules.name]" />
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同类型">
              <a-select placeholder="选择合同类型" v-decorator="[ 'type']">
                <a-select-option value="采购合同">采购合同</a-select-option>
                <a-select-option value="销售合同">销售合同</a-select-option>
              </a-select>
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同金额">
              <a-input placeholder="输入合同金额" v-decorator.trim="[ 'total']" />
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="需方单位">
              <a-select placeholder="选择需方单位" v-decorator="[ 'companyName']" allowClear>
                <a-select-option value="四川程邦制药工程有限公司">四川程邦制药工程有限公司</a-select-option>
                <a-select-option value="四川程邦制药设备有限公司">四川程邦制药设备有限公司</a-select-option>
                <a-select-option value="四川程德机械设备有限公司">四川程德机械设备有限公司</a-select-option>
                <a-select-option value="成都顾特科技有限公司">成都顾特科技有限公司</a-select-option>
              </a-select>
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="签约时间">
              <j-date v-decorator="['signTime']" :show-time="true"/>
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属项目">
              <a-select showSearch optionFilterProp="children"  v-decorator="[ 'projectId', validatorRules.projectId ]"
                        placeholder="请选择" allowClear>
                <a-select-option v-for="(item,index) in projectList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="供应商">
              <a-select showSearch optionFilterProp="children"  v-decorator="[ 'supplierId', validatorRules.supplierId ]"
                        placeholder="请选择" allowClear>
                <a-select-option v-for="(item,index) in supplierList" :key="index" :value="item.id">
                  {{ item.supplier }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-row>

          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
              <a-textarea :rows="5" placeholder="请输入备注" v-decorator.trim="[ 'description' ]" />
            </a-form-item>
          </a-row>
          <a-row class="form-row" :gutter="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="附件">
              <j-upload v-model="fileList" bizPath="contract"></j-upload>
            </a-form-item>
          </a-row>

        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import {
  addContract, checkProject, editContract,
  findBySelectContract, findBySelectProject, findBySelectSup

} from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
import JUpload from '@comp/jeecg/JUpload.vue'
import JDate from '@comp/jeecg/JDate.vue'

export default {
  name: 'ContractModal',
  components: { JUpload,JDate },
  mixins: [mixinDevice],
  data () {
    return {
      fileList: [],
      projectList: [],
      supplierList: [],
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' },
          ]
        },
        sn: {
          rules: [
            { required: true, message: '请输入编号!' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' },
          ]
        },
        projectId:{
          rules: [
            { required: false, message: '请选择项目!' }
          ]
        },
      }
    }
  },
  created () {
    this.initProject()
  },
  methods: {
    add () {
      this.edit({})
      this.fileList = [];
    },
    edit (record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.fileList = this.model.attache
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'name', 'type','total', 'sn','projectId'
          ,'supplierId','signTime','companyName','description'))
        autoJumpNextInput('contractModal')
      })
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    initProject () {
      let that = this
      findBySelectProject({}).then((res) => {
        if (res) {
          that.projectList = res
        }
      })

      findBySelectSup({}).then((res)=>{
        if ( res ){
          that.supplierList = res;
          // for (let i = 0; i < res.length; i++) {
          //   let temp = res[i];
          //   that.supplierList.push(temp);
          // }
          // console.log(that.supplierList);
        }
      });
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          formData.attache = this.fileList
          let obj
          if (!this.model.id) {
            obj = addContract(formData)
          } else {
            obj = editContract(formData)
          }
          obj.then((res) => {
            if (res.code === 200) {
              that.$emit('ok')
            } else {
              that.$message.warning(res.data.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel () {
      this.close()
    },

  }
}
</script>
<style scoped>

</style>